package com.kgcx.organization.service;

import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.organization.domain.AutoModel;
import com.kgcx.organization.mapper.AutoModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AutoModelService {
    @Autowired
    private AutoModelMapper autoModelMapper;

    public AutoModel getAutoModel(Long kid) {
        return autoModelMapper.selectByPrimaryKey(kid);
    }

    public List<AutoModel> getAutoModelList(RequestData rd) {
        PageUtils.page(rd);
        return autoModelMapper.selectAllActive();
    }

    @Transactional(rollbackFor = Exception.class)
    public void addAutoModel(AutoModel autoModel) {
        autoModelMapper.insertSelective(autoModel);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateAutoModel(AutoModel autoModel) {
        autoModelMapper.updateByPrimaryKeySelective(autoModel);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeAutoModel(Long kid) {
        AutoModel autoModel = new AutoModel();
        autoModel.setKid(kid);
        autoModel.setStatus(Cons.COMMON_STATUS_DELETE);
        autoModelMapper.updateByPrimaryKeySelective(autoModel);
    }

    public String getModelNameById(Long kid) {
        AutoModel autoModel = getAutoModel(kid);
        if (null != autoModel) {
            return autoModel.getName();
        }
        return null;
    }
}
